package com.cx.core.magazine.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cx.core.magazine.model.bean.Period;
import com.cx.core.magazine.service.MagazineManageService;
import com.cx.core.magazine.util.JSONTool;
import com.cx.core.magazine.util.MagTool;
import com.li.core.RequestEnvelope;
import com.li.core.ResponseEnvelope;
import com.li.core.actionBase.ActionBase;

public class ViewManageAction extends ActionBase {
	public ActionForward queryTreeNodes(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ResponseEnvelope<String, String> rse = new ResponseEnvelope<String, String>();
		MagazineManageService ms = (MagazineManageService) super
				.getBean("magazineManageService");
		RequestEnvelope<String, String> rqs = this.splitParameters(request);
		String nodeId = rqs.getParameter("id");
		//默认输出目录结构
		String rootTree = "";
		if (nodeId.equals("root")) {
			rootTree = "[{id:'1',text:'报道期刊管理',expanded: false},{id:'2',text:'系统管理',expanded:false}]";
		} else if (nodeId.equals("1")) {
			ArrayList<String> nodes = ms.queryYears();
			int size = nodes.size();
			StringBuffer sb = new StringBuffer("[");
			for (int i=0;i<size;i++){
				String text = (String) nodes.get(i);
				sb.append("{id:'article-");
				sb.append(text);
				sb.append("',text:'");
				sb.append(text);
				sb.append("'}");
				if ( i != (size -1)){
					sb.append(",");
				}
			}
			sb.append("]");
			rootTree = sb.toString();
		} else if (nodeId.equals("2")) {
			rootTree = "[{ text:'用户管理',id:'user-manage',leaf:true}]";
		} else {
			int year = Integer.parseInt(nodeId.substring(nodeId.indexOf("-")+1));
			List<Period> periods = ms.queryPeriods(year);
			StringBuffer sb = new StringBuffer("[");
			int i=0;
			for(Period p:periods){
				i++;
				sb.append("{id: 'article-");
				sb.append(p.getPeriod_id());
				sb.append("_");
				sb.append(p.getPeriod());
				sb.append("',text:'");
				sb.append(year);
				sb.append("-");
				sb.append(p.getYear_period());
				sb.append("',leaf : true}");
				if (i != periods.size()){
					sb.append(",");
				}
			}
			sb.append("]");
			rootTree = sb.toString();
		}
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(rootTree);
		return null;
	}
	
	public ActionForward queryAllArticles(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ResponseEnvelope<String, String> rse = new ResponseEnvelope<String, String>();
		RequestEnvelope<String, String> rqs = this.splitParameters(request);
		rqs.addParameter("loadimageflag", "true");
		MagazineManageService ms = (MagazineManageService) super
				.getBean("magazineManageService");
		
		rse = ms.queryFrontArticlesByPeriod(rqs);
		String articleJson= rse.getParameter("aritcleForm");
		StringBuffer sb = new StringBuffer("{success:true,root:");
		sb.append(articleJson);
		sb.append("}");
		response.setCharacterEncoding("utf-8");
		System.out.println(sb.toString());
		response.getWriter().write(sb.toString());
		return null;
	}
	
	public ActionForward queryYears(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MagazineManageService ms = (MagazineManageService) super
				.getBean("magazineManageService");
		ArrayList<String> years = ms.queryYears();
		StringBuffer sb = new StringBuffer("{success:true,root:[");
		int i = 0;
		for (String year : years){
			i++;
			sb.append("{\"year\":");
			sb.append(year);
			sb.append("}");
			if (i != years.size()){
				sb.append(",");
			}
		}
		//sb.append(JSONTool.ObjectToJSON(years));
		
		sb.append("]}");
		response.setCharacterEncoding("utf-8");

		System.out.println(sb.toString());
		response.getWriter().write(sb.toString());
		return null;
	}
	
}
